package controller;

import databaseLayer.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import movieBean.*;
import msgDisplayBean.*;
import pageBean.PageDetail;
import userBean.*;

/**
 *
 * @author Sam
 */
public class IndexController extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserIdentity userIdentity;
        PageDetail pageDetail = new PageDetail();
        Movie[] availableMovies;
        Date currentTime = new Date();
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy'/'MM'/'dd HH:mm:ss z");
        SimpleDateFormat timeZoneFormatter = new SimpleDateFormat("z");
        String address = "/index.jsp";
        
        HttpSession session = request.getSession();
        if (session.isNew()) {
            //Identify user as a visitor
            userIdentity = new Visitor();
            session.setAttribute("userIdentity", (Visitor)userIdentity);
        }
        
        //Identify the user identity
        userIdentity = (UserIdentity)session.getAttribute("userIdentity");
        
        //Check login request only if user is still a visitor
        if (userIdentity.getUserType().equals("Visitor")) {
            LoginMsg loginMsgDisplay = new LoginMsg();
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            if ((username == null || username == "") && (password == null || password == "")) {
                loginMsgDisplay.setDisplay(false);
            }
            else {
                loginMsgDisplay.setDisplay(true);
                
                if (username == null || username == "") {
                    loginMsgDisplay.setDisplayMsg("Username is missing!");
                }
                else if (password == null || password == "") {
                    loginMsgDisplay.setDisplayMsg("Password is missing!");
                }
                else {
                    userIdentity = UserLookUp.getUserIdentity(username, password);
                    
                    if (userIdentity.getUserType().equals("RegisteredCustomer")) {
                        CustomerUpdate.userLogin(username);
                        session.setAttribute("userIdentity", (RegisteredCustomer)userIdentity);
                        session.setAttribute("userName", username);
                        loginMsgDisplay.setDisplayMsg("Successfully login as " + ((RegisteredCustomer)userIdentity).getId() + "!");
                        
                        request.setAttribute("userName", username);
                        request.setAttribute("userRealName", ((RegisteredCustomer)userIdentity).getName());
                        request.setAttribute("loyaltyPoint", ((RegisteredCustomer)userIdentity).getLoyaltyPoint());
                        session.setMaxInactiveInterval(10*60);
                    }
                    else if (userIdentity.getUserType().equals("TicketingOfficer")) {
                        CustomerUpdate.userLogin(username);
                        session.setAttribute("userIdentity", (TicketingOfficer)userIdentity);
                        session.setAttribute("userName", username);
                        loginMsgDisplay.setDisplayMsg("Successfully login as ticketing officer " + ((TicketingOfficer)userIdentity).getId() + "!");
                        
                        session.setMaxInactiveInterval(10*60);
                    }
                    else if (userIdentity.getUserType().equals("TicketingManager")) {
                        CustomerUpdate.userLogin(username);
                        session.setAttribute("userIdentity", (TicketingManager)userIdentity);
                        session.setAttribute("userName", username);
                        loginMsgDisplay.setDisplayMsg("Successfully login as ticketing manager " + ((TicketingManager)userIdentity).getId() + "!");
                        
                        session.setMaxInactiveInterval(10*60);
                    }
                    else if (userIdentity.getUserType().equals("BreakInUser")) {
                        userIdentity = new Visitor();
                        loginMsgDisplay.setDisplayMsg("You cannot login to the system in different locations at the same time!");
                    }
                    else {
                        loginMsgDisplay.setDisplayMsg("Either username or password is incorrect!");
                    }
                }
            }

            request.setAttribute("loginMsgDisplay", loginMsgDisplay);
        } else if (userIdentity.getUserType().equals("RegisteredCustomer")) {
            request.setAttribute("userName", ((RegisteredCustomer)userIdentity).getId());
            request.setAttribute("userRealName", ((RegisteredCustomer)userIdentity).getName());
            request.setAttribute("loyaltyPoint", ((RegisteredCustomer)userIdentity).getLoyaltyPoint());
        } else if (userIdentity.getUserType().equals("TicketingOfficer")) {
            request.setAttribute("userName", ((TicketingOfficer)userIdentity).getId());
        }
        
        //Forward Ticketing Manager to another page
        if (userIdentity.getUserType().equals("TicketingManager")) {
            address = "Manage.do";
        }
        
        String beginTime = dateFormatter.format(currentTime);
        String timeZone = timeZoneFormatter.format(currentTime);
        
        //Set up the page for user with different identity
        if (userIdentity.getUserType().equals("Visitor")) {
            pageDetail.setHeaderBar("/mainNavigationBar.jsp");
            
            SimpleDateFormat todayFormatter = new SimpleDateFormat("yyyy'/'MM'/'dd");
            String today = todayFormatter.format(currentTime);
            
            String endTime = today + " 23:59:59 " + timeZone;
            
            availableMovies = MoviesLookUp.getAllMovies(beginTime, endTime);
        }
        else {
            String endTime = "9999/99/99 23:59:59 " + timeZone;
            availableMovies = MoviesLookUp.getAllMovies(beginTime, endTime);
            
            if (userIdentity.getUserType().equals("RegisteredCustomer")) {
                pageDetail.setHeaderBar("/loginNavigationBar.jsp");
            }
            else if (userIdentity.getUserType().equals("TicketingOfficer")) {
                pageDetail.setHeaderBar("/ticketOfficerNavigationBar.jsp");
                request.setAttribute("isOfficer", "Yes");
            }
        }
        
        request.setAttribute("pageDetail", pageDetail);
        request.setAttribute("availableMovies", availableMovies);
        
        int noOfMovie = availableMovies.length;
        request.setAttribute("noOfMovie", noOfMovie);
        
        String refund = request.getParameter("action");
        if (refund != null && refund.equals("refund")) {
            request.setAttribute("startPage", 5);
        } else if (refund != null && refund.equals("approve")) {
            request.setAttribute("startPage", 2);
        } else {
            request.setAttribute("startPage", 1);
        }
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}